package com.itwn.comic.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itwn.comic.entity.Remark;
import com.itwn.comic.entity.Report;
import com.itwn.comic.mapper.ReportMapper;
import com.itwn.comic.vo.ReportVo;
import com.itwn.comic.reult.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

import java.util.List;

/**
 * @author cx
 * @date 2022/2/19
 * @apiNote
 */
@Service
public class ReportService {
    @Autowired
    ReportMapper reportMapper;

    /**
     *
     * 查询举报
     * @param pageNum
     * @param pageSize
     * @param status
     * @param reportType 1 评论举报 2 资源举报
     * @param reportUserId
     * @param resourceId
     * @return
     */
    public PageInfo<ReportVo> query(Integer pageNum, Integer pageSize, Integer status, Integer reportType, Integer reportUserId, Integer resourceId) {
        PageHelper.startPage(pageNum,pageSize);
        List<ReportVo> list = new ArrayList<>();
        List<Integer> listIds = new ArrayList<>();
        if(reportType==1){
            list=reportMapper.selectRemark(status,reportType,reportUserId);
        }else if(reportType==2){
            list=reportMapper.selectComic(status,reportType,reportUserId,resourceId);
        }
        for (ReportVo reportVo : list) {
            listIds.add(reportVo.getId());
        }
        //如果查询的为statue 0 则置为 1
        if(status==0 && list!=null && !list.isEmpty()){
            reportMapper.updateStatus(listIds,0,1);
        }

        return new PageInfo<>(list);
    }
    /**
     * 分页查询举报的漫画
     * @param now
     * @param size
     * @return
     */
    public PageInfo<Report> queryComicByPage(Integer now, Integer size) {
        PageHelper.startPage(now,size);
        List<Report> list = this.reportMapper.selectAllComic();
        return new PageInfo<>(list);
    }
    /**
     * 分页查询举报
     * @param now
     * @param size
     * @return
     */
    public PageInfo<Report> queryRemarkByPage(Integer now, Integer size) {
        PageHelper.startPage(now,size);
        List<Report> list = this.reportMapper.selectAllRemark();
        return new PageInfo<>(list);
    }

    /**
     * 添加
     * @param report
     */
    public void add(Report report) {
        this.reportMapper.insert(report);
    }

    /**
     * 修改
     * @param report
     */
    public void modify(Report report) {
        this.reportMapper.updateByPrimaryKey(report);
    }
    /**
     * 删除,已处理方法的业务也在这(待完善)
     */
    public void removeByKey(long id) {
        this.reportMapper.deleteByPrimaryKey(id);
    }

    /**
     *
     * @param ids
     * @param fromStatus
     * @param ToStatus
     */
    public void updateStatus(String ids,Integer fromStatus,Integer ToStatus) {
        String[] split = ids.split("-");
        ArrayList<Integer> list = new ArrayList<>();
        for (String s : split) {
            list.add(Integer.parseInt(s));
        }
        reportMapper.updateStatus(list,fromStatus,ToStatus);
    }
}
